import { reactive } from 'vue'
import { refresh, getprintData, savePrintTime } from '@/axios/api/sign_bill/qd_print'
import { ElNotification } from 'element-plus';
import { useSignStore } from '@/store/sign';
const signStore = useSignStore();

//@ts-ignore
import { throttle } from 'lodash'; // 防抖函数


export default function () {
    // 表格数据
    // let tableData = ref([])
    let printData = reactive({
        table: [],
        con: "",
        cid: "",
        house: "",
        whouse: "",
        position: "",
        xs: 0,
        qty_s: 0,
        LotNoDate: "",
        LotNo: ""
    });
    // 获取刷新表格数据
    async function getTableData(cid: string, con: string) {
        signStore.refreshData_pinia=[]
        try {
            let res: any = await refresh({
                cid: cid,
                con: con,
            })
            signStore.refreshData_pinia = JSON.parse(res.data);
            // signStore.refreshData_pinia=tableData.value
            return 'ok';
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
            return Promise.reject(new Error(error))
        }
    }

    // 定义一个接口来描述 row 的类型
    interface Row {
        Item: string;
        Qty: number;
    }
    // 获取报表表格数据
    async function getPrintData(flag: Boolean, row: any) {
        printData.table = [];
        printData.con = "";
        printData.cid = "";
        printData.house = "";
        printData.whouse = "";
        printData.position = "";
        printData.xs = 0;
        printData.qty_s = 0;
        printData.LotNoDate = "";
        printData.LotNo = "";
        try {
            let res: any = await getprintData({
                flag: flag,
                cid: row.Con_PickingID,
            })
            printData.table = JSON.parse(res.data);
            printData.con = row.ContractNO;
            printData.cid = row.Con_PickingID;
            printData.house = row.Classify;
            printData.whouse = row.Warehouse;
            printData.position = row.Position;
            printData.LotNoDate = row.LotNoDate;
            printData.LotNo = row.LotNo;
            // 使用一个对象来存储每个Item的Qty
            const itemMap: { [key: string]: number } = {};
            // 遍历数据，计算每个Item的Qty总和
            printData.table.forEach((row: Row) => {
                if (!itemMap[row.Item]) {
                    itemMap[row.Item] = row.Qty;
                }
            });
            // 计算不重复的Item数量
            printData.xs = Object.keys(itemMap).length;
            // 计算每个Item的Qty总和
            printData.qty_s = Object.values(itemMap).reduce((sum, qty) => sum + qty, 0);
            return 'ok';
        } catch (error: any) {
            // ElNotification({
            //     title: 'Error',
            //     message: error,
            //     type: 'error',
            // })
            // return Promise.reject(new Error(error))
            return '没有数据'
        }

    }

    // 保存打印时间
    async function printTime(Con_PickingID: string) {
        try {
            await savePrintTime({
                cid: Con_PickingID,
            })
            return 'ok';
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
            return Promise.reject(new Error(error))
        }
    }


    return {
        getTableData,
        getPrintData,
        printData,
        printTime
    }
}